home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / lib / exiftool.py < prev    next >
Encoding:
Python Source  |  2012-05-27  |  3.1 KB  |  96 lines

  1. '''
  2.     Care about images with help of the amazing (perl) library Exiftool.
  3. '''
  4.  
  5. import subprocess
  6. import parser
  7.  
  8.  
  9. class ExiftoolStripper(parser.GenericParser):
  10.     '''
  11.         A generic stripper class using exiftool as backend
  12.     '''
  13.  
  14.     def __init__(self, filename, parser, mime, backup, add2archive):
  15.         super(ExiftoolStripper, self).__init__(filename, parser, mime,
  16.         backup, add2archive)
  17.         self.allowed = ['ExifTool Version Number', 'File Name', 'Directory',
  18.                 'File Size', 'File Modification Date/Time', 'File Permissions',
  19.                 'File Type', 'MIME Type', 'Image Width', 'Image Height',
  20.                 'Image Size']
  21.         self._set_allowed()
  22.  
  23.     def _set_allowed(self):
  24.         '''
  25.             Set the allowed/harmless list of metadata
  26.         '''
  27.         raise NotImplementedError
  28.  
  29.     def remove_all(self):
  30.         '''
  31.             Remove all metadata with help of exiftool
  32.         '''
  33.         try:
  34.             if self.backup:
  35.                 # Note: '-All=' must be followed by a known exiftool option.
  36.                 process = subprocess.Popen(['exiftool', '-m', '-All=',
  37.                     '-out', self.output, self.filename],
  38.                     stdout=open('/dev/null'))
  39.                 process.wait()
  40.             else:
  41.                 # Note: '-All=' must be followed by a known exiftool option.
  42.                 process = subprocess.Popen(
  43.                     [ 'exiftool', '-m', '-All=', '-overwrite_original', self.filename ],
  44.                     stdout=open('/dev/null'))
  45.                 process.wait()
  46.             return True
  47.         except:
  48.             return False
  49.  
  50.     def is_clean(self):
  51.         '''
  52.             Check if the file is clean with help of exiftool
  53.         '''
  54.         out = subprocess.Popen(['exiftool', self.filename],
  55.                 stdout=subprocess.PIPE).communicate()[0]
  56.         out = out.split('\n')
  57.         for i in out[:-1]:
  58.             if i.split(':')[0].strip() not in self.allowed:
  59.                 return False
  60.         return True
  61.  
  62.     def get_meta(self):
  63.         '''
  64.             Return every harmful meta with help of exiftool
  65.         '''
  66.         out = subprocess.Popen(['exiftool', self.filename],
  67.                 stdout=subprocess.PIPE).communicate()[0]
  68.         out = out.split('\n')
  69.         meta = {}
  70.         for i in out[:-1]:
  71.             key = i.split(':')[0].strip()
  72.             if key not in self.allowed:
  73.                 meta[key] = i.split(':')[1].strip()
  74.         return meta
  75.  
  76.  
  77. class JpegStripper(ExiftoolStripper):
  78.     '''
  79.         Care about jpeg files with help
  80.         of exiftool
  81.     '''
  82.     def _set_allowed(self):
  83.         self.allowed.extend(['JFIF Version', 'Resolution Unit',
  84.         'X Resolution', 'Y Resolution', 'Encoding Process', 'Bits Per Sample',
  85.         'Color Components', 'Y Cb Cr Sub Sampling'])
  86.  
  87. class PngStripper(ExiftoolStripper):
  88.     '''
  89.         Care about png files with help
  90.         of exiftool
  91.     '''
  92.     def _set_allowed(self):
  93.         self.allowed.extend(['Bit Depth', 'Color Type', 'Compression',
  94.             'Filter', 'Interlace', 'Pixels Per Unit X', 'Pixels Per Unit Y',
  95.             'Pixel Units'])
  96.